﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PBOffice.Models;
using System.Data;
using System.Data.SqlClient;


namespace PBOffice.DAL
{
    public class OperateLogService
    {
        public List<OperateLog> GetAllOperateLog()
        {
            List<OperateLog> list = new List<OperateLog>();
            string sql = "select * from OperateLog";
            using (IDataReader dr = SqlHelp.ExecuteReader(SqlHelp.connectionstring, CommandType.Text, sql))
            {
                while (dr.Read())
                {
                    OperateLog log = new OperateLog();
                    log.OperateId = (int)dr["OperateId"];
                    log.UserId = dr["UserId"].ToString();
                    log.OperateName = dr["OperateName"].ToString();
                    log.OperateTime = Convert.ToDateTime(dr["OperateTime"]);
                    log.OperateDesc = dr["OperateDesc"].ToString();
                    log.ObjectId = dr["ObjectId"].ToString();
                    list.Add(log);
                }
            }
            return list;
        }
        public List<OperateLog> GetAllOperateLog(DateTime begin, DateTime end)
        {
            List<OperateLog> list = new List<OperateLog>();
            string sql = string.Format("select * from LoginLog where OperateTime Between '{0}' and '{1}'", begin, end);
            using (IDataReader dr = SqlHelp.ExecuteReader(SqlHelp.connectionstring, CommandType.Text, sql))
            {
                while (dr.Read())
                {
                    OperateLog log = new OperateLog();
                    log.OperateId = (int)dr["OperateId"];
                    log.UserId = dr["UserId"].ToString();
                    log.OperateName = dr["OperateName"].ToString();
                    log.OperateTime = Convert.ToDateTime(dr["OperateTime"]);
                    log.OperateDesc = dr["OperateDesc"].ToString();
                    log.ObjectId = dr["ObjectId"].ToString();
                    list.Add(log);
                }
            }
            return list;
        }
        public void DeleteOperateLog(int id)
        {
            string sql = string.Format("delete [LoginLog] where OperateId={0}", id);
            SqlHelp.ExecuteNonQuery(SqlHelp.connectionstring, CommandType.Text, sql);
        }
    }
}
